/*******************************************************************************
 * Copyright (c) 2007 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.sequence.ui.validators;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.ui.dialogs.ISelectionStatusValidator;
import org.eclipse.uml2.uml.ConnectableElement;
import org.eclipse.uml2.uml.InstanceSpecification;
import org.eclipse.uml2.uml.Type;

import com.cea.papyrus.core.PapyrusPlugin;

/**
 * A validator class for transformation selection dialog
 *
 */
public class InstanceSpecificationSelectionStatusValidator implements
		ISelectionStatusValidator {

	private ConnectableElement connectableElement;
	
	public InstanceSpecificationSelectionStatusValidator(ConnectableElement connectableElement) {
		super();
		this.connectableElement = connectableElement;
	}
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.dialogs.ISelectionStatusValidator#validate(java.lang.Object[])
	 */
	public IStatus validate(Object[] arg0) {
		
		if ((arg0 != null) && (arg0.length > 0)) {
			// Single selection
			Object selection = arg0[0];
			
			if ( !(selection instanceof InstanceSpecification)) {
				// Bad selection
				return new Status(IStatus.ERROR, PapyrusPlugin.PLUGIN_ID, "Select an InstanceSpecification.");
			} else if (selection instanceof InstanceSpecification) {
				
				InstanceSpecification instance = (InstanceSpecification) selection;
				
				if (instance.getClassifiers() != null) {
					if (connectableElement.getType() != null) {
						Type expectedType = connectableElement.getType();
						if (instance.getClassifiers().contains(expectedType)) {
							return Status.OK_STATUS;
						}
					}
				}
				String msg = 
					"Should be an instance of : "+connectableElement.getType().getLabel()+".";
				return new Status(IStatus.WARNING, PapyrusPlugin.PLUGIN_ID, msg);
			}
		} else {
			// No selection
			return new Status(IStatus.WARNING, PapyrusPlugin.PLUGIN_ID, "Empty selection.");
		}
		
		return new Status(IStatus.ERROR, PapyrusPlugin.PLUGIN_ID, "Unknown error.");
	}

}
